<?php

namespace app\controllers\h5\my;

use app\controllers\h5\base\H5Controller;
use app\models\admin_ops\AdminOpsRefill;
use app\models\Transfer;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserConsume;
use app\models\UserBalance;
use app\models\UserRegArea;

class AccountController extends H5Controller
{
    public function actionAccount()
    {
        $account = UserAccount::find()
            ->where(['user_id' => $this->user->id])
            ->asArray()
            ->one();

        $account['balance'] = floatval($account['balance']);
        $account['await_balance'] = floatval($account['await_balance']);

        return $this->successEncode(['account' => $account]);
    }

    // 互转
    public function actionTransfer()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = Transfer::find()
            ->where([
                'or',
                ['send_id' => $this->user->id],
                ['received_id' => $this->user->id],
            ]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            if ($item['send_id'] == $this->user->id) {
                $item['text'] = '转出';
                $item['ops'] = '-';
            } else {
                $item['text'] = '接收';
                $item['ops'] = '+';
            }
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionCommission()
    {
        $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserCommission::find()
            ->where(['status' => 1])
            ->andWhere(['user_id' => $this->user->id]);

        switch ($type) {
            case 'score':
                $query->andWhere(['>', 'score', 0]);
                break;
            case 'ticket':
                $query->andWhere(['>', 'ticket', 0]);
                break;
            case 'platform_ticket':
                $query->andWhere(['>', 'platform_ticket', 0]);
                break;
            case 'balance':
                $query->andWhere(['>', 'balance', 0]);
                break;
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionConsume()
    {
        $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserConsume::find()
            ->where(['status' => 1])
            ->andWhere(['user_id' => $this->user->id]);

        switch ($type) {
            case 'score':
                $query->andWhere(['>', 'score', 0]);
                break;
            case 'ticket':
                $query->andWhere(['>', 'ticket', 0]);
                break;
            case 'platform_ticket':
                $query->andWhere(['>', 'platform_ticket', 0]);
                break;
            case 'balance':
                $query->andWhere(['>', 'balance', 0]);
                break;
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    // 申请区代
    public function actionRegArea()
    {
        $model = UserRegArea::findOne(['user_id' => $this->user->id]);
        if ($model) {
            if ($model->status == 0) {
                return $this->fail('正在审核中,请耐心等待...');
            }
            if ($model->status == 1) {
                return $this->fail('您已经通过审核...');
            }
            if ($model->status == 2) {
                return $this->fail('您已经被拒绝,请联系客服!');
            }
        }

        $model = new UserRegArea();
        $model->user_id = $this->user->id;
        $model->save();

        return $this->success();
    }

    // 后台充值记录
    public function actionRefill()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = AdminOpsRefill::find()
            ->andWhere(['user_id' => $this->user->id]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    // 余额明细
    public function actionBalanceLog()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserBalance::find()
            ->andWhere(['user_id' => $this->user->id]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
}